using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class Player : MonoBehaviour 
{

	public Rigidbody2D rigidbodyBird;

	public float liftForce;

	public Animator animator;

	private bool death = false;

	public delegate void DeathNotify();

	public event DeathNotify OnDeath;

	public UnityAction<int> OnScore;

	private Vector3 initPos;

	// Use this for initialization
	void Start ()
	{
		this.Idle();
		initPos = transform.position;
	}

	// Update is called once per frame
	void Update ()
	{
        if (death)
        {
			return;
        }

        if (Input.GetMouseButtonDown(0))
        {
			rigidbodyBird.velocity = Vector2.zero;
			rigidbodyBird.AddForce(new Vector2(0, liftForce), ForceMode2D.Impulse);
			this.Fly();
		}
	}

	public void Init()
	{
		this.transform.position = initPos;
		this.Idle();
		this.death = false;
	}

	void Die()
	{
		this.death = true;
		//this.Idle(); //小鸟撞死时还会悬在天上
		this.animator.SetTrigger("Idle");

		if (this.OnDeath != null)
        {
			this.OnDeath();
        }
	}

	void Idle()
	{

		this.rigidbodyBird.simulated = false;
		this.animator.SetTrigger("Idle");
	}
	void Fly()
	{

		this.rigidbodyBird.simulated = true;
		this.animator.SetTrigger("Fly");
	}

	void OnTriggerEnter2D(Collider2D col)
	{
		Debug.Log("OnTriggerEnter2D:" + col.gameObject.name + " : " + gameObject.name + " : " + Time.time);
        if (col.gameObject.name.Equals("ScoreArea"))
        {
			return;
        }
		else
			this.Die();
	}

	void OnTriggerExit2D(Collider2D col)
	{
		Debug.Log("OnTriggerExit2D:" + col.gameObject.name + " : " + gameObject.name + " : " + Time.time);
		if (col.gameObject.name.Equals("ScoreArea"))
		{
            if (this.OnScore != null)
            {
				this.OnScore(1);

			}
		}
	}

	void OnCollisionEnter2D(Collision2D col)
    {
        Debug.Log("OnCollisionEnter2D:" + col.gameObject.name + " : " + gameObject.name + " : " + Time.time);
        this.Die();
    }
}
